# Maintainer: Giancarlo Razzolini <grazzolini@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Dave Reisner <dreisner@archlinux.org>
# Contributor: Thomas Bächler <thomas@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - removed -mtune, changed -march to v8/v7/v6/v5 compat

pkgname=mkinitcpio-busybox
pkgver=1.32.1
pkgrel=3
pkgdesc='Base initramfs tools'
arch=(x86_64)
url="https://www.busybox.net/"
license=(GPL)
depends=(glibc libxcrypt)
options=(!buildflags)
source=(https://busybox.net/downloads/busybox-$pkgver.tar.bz2{,.sig}
        0001-ash-fix-unset_var-pattern-repl.patch
        0002-decompress_gunzip-Fix-DoS-if-gzip-is-corrupt.patch
        config)
sha256sums=('9d57c4bd33974140fd4111260468af22856f12f5b5ef7c70c8d9b75c712a0dee'
            'SKIP'
            '4e4fb268c51e378a3ad0a12f903a601a136d31cae8b684a51ebabbc9a6cf9250'
            'cdc11deae6bb32d0994a252d545f6624513f7f46e44e9dca259f243fc87f598f'
            '81843d10a63f3e29f37560ded54a049b45c8a4b38560bf9489ec4124eead170e')
validpgpkeys=('C9E9416F76E610DBD09D040F47B70C55ACC9965B') # Denis Vlasenko <vda.linux@googlemail.com>

prepare() {
  cd "busybox-$pkgver"

  patch -Np1 < ../0001-ash-fix-unset_var-pattern-repl.patch
  patch -Np1 < ../0002-decompress_gunzip-Fix-DoS-if-gzip-is-corrupt.patch

  if [ ${CARCH} = "aarch64" ]; then
    ARMCARCH="armv8-a"
  elif [ ${CARCH} = "armv7h" ]; then
    ARMCARCH="armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16"
  elif [ ${CARCH} = "armv6h" ]; then
    ARMCARCH="armv6 -mfloat-abi=hard -mfpu=vfp"
  else
    ARMCARCH="armv5te"
  fi

  local safeflags="-march=${ARMCARCH} -Os -pipe -fno-strict-aliasing"

  sed 's|^\(CONFIG_EXTRA_CFLAGS\)=.*|\1="'"$safeflags"'"|' "$srcdir/config" > .config
}

build() {
  # reproducible build
  export KCONFIG_NOTIMESTAMP=1
  make -C "busybox-$pkgver"
}

package() {
  install -Dm755 "$srcdir/busybox-$pkgver/busybox" "$pkgdir/usr/lib/initcpio/busybox"
}
